package com.studentexam.student.service.impl;

import com.studentexam.student.dao.RoleMapper;
import com.studentexam.student.dao.RolePersonPermissionMapper;
import com.studentexam.student.model.Role;
import com.studentexam.student.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePersonPermissionMapper rolePersonPermissionMapper;
    @Override
    public int addRole(Role role) {
        int returnInt=0;
//        1.数据判断
        int rolenameCount=roleMapper.getRolenameCount(role.getRolename());
        if (rolenameCount<1){
//        2.添加数据
            int addRolenameInt=roleMapper.insertRole(role);
//        3.设置返回值
            returnInt=addRolenameInt;
        }else returnInt=-1;
        return returnInt;
    }

    @Override
    public int updateRole(Role role) {
        int isUpdate=0;
        try{
            isUpdate=roleMapper.updateRole(role);
        }catch (Exception e){
            isUpdate=-1;
            System.out.println("更新角色时执行sql报错原因为:"+e.getMessage());
        }
        return isUpdate;
    }

    @Override
    public int delRole(int roleid) {
        int returnDel=0;
        try {
            if (rolePersonPermissionMapper.getRoleCount(roleid) < 1) {
                returnDel = roleMapper.delRole(roleid);
            }
        }catch (Exception e){
            returnDel=-1;
            System.out.println("删除失败原因为："+e.getMessage());
        }
        return returnDel;
    }

    @Override
    public List<Role> getByRoleid(int roleid) {
            List<Role> roleList=roleMapper.getByRoleid(roleid);
        return roleList;
    }

    @Override
    public List<Role> getAllRole(Role role){
            List<Role> roleList = roleMapper.getAllRole(role);
            return roleList;
    }
}
